package clientService;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OrdersFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtBtr;
	private JTextField textField;
	private JTextField textField_1;
	private JRadioButton rdbtnSell;
	private JRadioButton rdbtnBuy;
	private ButtonGroup group = new ButtonGroup();
	private JButton btnAddOrder;
	private static JTextPane txtpnOrders;
	private static String result;

	/**
	 * Launch the application.
	 */
	public static void main(String... args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OrdersFrame frame = new OrdersFrame();
					frame.setVisible(true);
					Thread thread = new Thread(new Runnable() {
						public void run() {
							while (true) {
								final String mess = ClientApp.recieveMessage();
								SwingUtilities.invokeLater(new Runnable() {
									public void run() {
										StyledDocument doc = txtpnOrders
												.getStyledDocument();
										SimpleAttributeSet keyWord = new SimpleAttributeSet();
										try {
											doc.insertString(doc.getLength(),
													"\n" + mess, keyWord);
										} catch (BadLocationException e) {
											e.printStackTrace();
										}
									}
								});
							}
						}
					});
					thread.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public OrdersFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 695, 446);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 1.0, Double.MIN_VALUE };
		contentPane.setLayout(gbl_contentPane);

		JLabel lblTicket = new JLabel("Ticket:");
		GridBagConstraints gbc_lblTicket = new GridBagConstraints();
		gbc_lblTicket.insets = new Insets(0, 0, 5, 5);
		gbc_lblTicket.anchor = GridBagConstraints.EAST;
		gbc_lblTicket.gridx = 1;
		gbc_lblTicket.gridy = 1;
		contentPane.add(lblTicket, gbc_lblTicket);

		txtBtr = new JTextField();
		txtBtr.setText("BTR");
		GridBagConstraints gbc_txtBtr = new GridBagConstraints();
		gbc_txtBtr.insets = new Insets(0, 0, 5, 0);
		gbc_txtBtr.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtBtr.gridx = 2;
		gbc_txtBtr.gridy = 1;
		contentPane.add(txtBtr, gbc_txtBtr);
		txtBtr.setColumns(10);

		JLabel lblAmount = new JLabel("Amount:");
		GridBagConstraints gbc_lblAmount = new GridBagConstraints();
		gbc_lblAmount.anchor = GridBagConstraints.EAST;
		gbc_lblAmount.insets = new Insets(0, 0, 5, 5);
		gbc_lblAmount.gridx = 1;
		gbc_lblAmount.gridy = 2;
		contentPane.add(lblAmount, gbc_lblAmount);

		textField = new JTextField();
		textField.setText("100");
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 2;
		gbc_textField.gridy = 2;
		contentPane.add(textField, gbc_textField);
		textField.setColumns(10);

		JLabel lblPrice = new JLabel("Price:");
		GridBagConstraints gbc_lblPrice = new GridBagConstraints();
		gbc_lblPrice.anchor = GridBagConstraints.EAST;
		gbc_lblPrice.insets = new Insets(0, 0, 5, 5);
		gbc_lblPrice.gridx = 1;
		gbc_lblPrice.gridy = 3;
		contentPane.add(lblPrice, gbc_lblPrice);

		textField_1 = new JTextField();
		textField_1.setText("50.0");
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.insets = new Insets(0, 0, 5, 0);
		gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_1.gridx = 2;
		gbc_textField_1.gridy = 3;
		contentPane.add(textField_1, gbc_textField_1);
		textField_1.setColumns(10);

		rdbtnSell = new JRadioButton("SELL");
		GridBagConstraints gbc_rdbtnSell = new GridBagConstraints();
		gbc_rdbtnSell.insets = new Insets(0, 0, 5, 0);
		gbc_rdbtnSell.gridx = 2;
		gbc_rdbtnSell.gridy = 4;
		rdbtnSell.setActionCommand("2");
		contentPane.add(rdbtnSell, gbc_rdbtnSell);

		rdbtnBuy = new JRadioButton("BUY");
		GridBagConstraints gbc_rdbtnBuy = new GridBagConstraints();
		gbc_rdbtnBuy.insets = new Insets(0, 0, 5, 0);
		gbc_rdbtnBuy.gridx = 2;
		gbc_rdbtnBuy.gridy = 5;
		rdbtnSell.setActionCommand("1");
		contentPane.add(rdbtnBuy, gbc_rdbtnBuy);

		group.add(rdbtnBuy);
		group.add(rdbtnSell);

		btnAddOrder = new JButton("Add Order");
		btnAddOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					public void run() {
						String type;
						if (rdbtnBuy.isSelected()) {
							type = "1";
						} else {
							type = "2";
						}
						result = ClientApp.newOrder(txtBtr.getText(),
								textField.getText(), textField_1.getText(),
								type);
						System.out.println("result " + result);
						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								StyledDocument doc = txtpnOrders
										.getStyledDocument();
								SimpleAttributeSet keyWord = new SimpleAttributeSet();
								try {
									doc.insertString(doc.getLength(), "\n"
											+ result, keyWord);
								} catch (BadLocationException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
					}
				}).start();
			}
		});
		GridBagConstraints gbc_btnAddOrder = new GridBagConstraints();
		gbc_btnAddOrder.insets = new Insets(0, 0, 5, 5);
		gbc_btnAddOrder.gridx = 1;
		gbc_btnAddOrder.gridy = 6;
		contentPane.add(btnAddOrder, gbc_btnAddOrder);

		txtpnOrders = new JTextPane();
		txtpnOrders.setText("Orders");
		GridBagConstraints gbc_txtpnOrders = new GridBagConstraints();
		gbc_txtpnOrders.fill = GridBagConstraints.BOTH;
		gbc_txtpnOrders.gridx = 2;
		gbc_txtpnOrders.gridy = 7;
		contentPane.add(txtpnOrders, gbc_txtpnOrders);
	}

}
